<script setup>
import { ref } from 'vue'
import { artGetChannelService, artDelChannelService } from '@/api/article'
import { Delete, Edit } from '@element-plus/icons-vue'
import channelEdit from './components/channelEdit.vue'
// import { ElMessage, ElMessageBox } from 'element-plus'
const channelList = ref([])
const loading = ref(false)
const dialog = ref()
const getChannel = async () => {
  loading.value = true
  const res = await artGetChannelService()
  channelList.value = res.data.data
  loading.value = false
}
getChannel()
const onEditChannel = (row) => {
  // console.log(row)
  dialog.value.open(row)
}
const onDelChannel = async (row) => {
  await ElMessageBox.confirm('确认要删除吗？', 'Warning', {
    confirmButtonText: '是',
    cancelButtonText: '否',
    type: 'warning'
  })
  await artDelChannelService(row)
  ElMessage.success('删除成功!')
  getChannel()
}
const addEditChannel = () => {
  dialog.value.open({})
}
const onSuccess = () => {
  getChannel()
}
</script>
<template>
  <page-container title="文章分类">
    <template #extra>
      <el-button type="primary" @click="addEditChannel">添加分类</el-button>
    </template>
    <el-table :data="channelList" style="width: 100%" v-loading="loading">
      <el-table-column type="index" width="100" label="序号" />
      <el-table-column prop="cate_name" label="分类名称"></el-table-column>
      <el-table-column prop="cate_alias" label="分类别名"></el-table-column>
      <el-table-column label="操作" width="180">
        <!-- 从传回来的参数中解构出row, channel是一个对象数组，row就是这个数组里的每一个值（也就是对象）,$index就是编号（其实就类似于v-for中的item和index）-->
        <template #default="{ row }">
          <div style="display: flex; align-items: center">
            <el-button
              type="primary"
              plain
              circle
              @click="onEditChannel(row)"
              :icon="Edit"
            ></el-button>
            <el-button
              type="danger"
              plain
              circle
              @click="onDelChannel(row)"
              :icon="Delete"
            ></el-button>
          </div>
        </template>
      </el-table-column>
      <template #empty>
        <el-empty description="description" />
      </template>
    </el-table>
    <channelEdit ref="dialog" @success="onSuccess"></channelEdit>
  </page-container>
</template>

<style scoped lang="scss"></style>
